package bioinfo.stats;

import bioinfo.*;
import javax.swing.*;
import java.awt.*;
import java.util.Properties;
import java.util.Hashtable;
import java.util.Enumeration;

import com.approximatrix.charting.swing.*;
import com.approximatrix.charting.model.*;
import com.approximatrix.charting.render.*;
import com.approximatrix.charting.*;

public class First extends Stats {
    private JPanel jp;

    public First() {
        int nb_a = 0;
        int nb_c = 0;
        int nb_g = 0;
        int nb_t = 0;
        int nb_others = 0;

        int max_a = 0;
        int max_c = 0;
        int max_g = 0;
        int max_t = 0;

        int min_a = 1000;
        int min_c = 1000;
        int min_g = 1000;
        int min_t = 1000;

        double moyenne_length = 0;
        int min_length = 1000;
        int max_length = 0;
        int nb = 0;

        int nb_e_c = 0;
        int nb_a_c = 0;
        int nb_b_c = 0;

        for(ARN arn_tmp : Main.arns) {
            String seq = arn_tmp.getSeq();
            int seq_a = 0;
            int seq_c = 0;
            int seq_g = 0;
            int seq_t = 0;

            if(arn_tmp.getCateg().equals("Eukarya")) {
                nb_e_c++;
            }
            else if(arn_tmp.getCateg().equals("Archaea")) {
                nb_a_c++;
            }
            else {
                nb_b_c++;
            }

            moyenne_length = ((moyenne_length*nb)+seq.length())/(++nb);
            min_length = (seq.length() < min_length) ? seq.length() : min_length;
            max_length = (seq.length() > max_length) ? seq.length() : max_length;

            for(int i = 0; i < seq.length(); i++) {
                switch(seq.charAt(i)) {
                    case 'A':
                    case 'a':
                        nb_a++;
                        seq_a++;
                        break;
                    case 'C':
                    case 'c':
                        nb_c++;
                        seq_c++;
                        break;
                    case 'G':
                    case 'g':
                        nb_g++;
                        seq_g++;
                        break;
                    case 'T':
                    case 't':
                        nb_t++;
                        seq_t++;
                        break;
                    default:
                        nb_others++;
                        break;
                }
            }

            if(seq_a < min_a) {
                min_a = seq_a;
            }
            else if(seq_a > max_a) {
                max_a = seq_a;
            }
            if(seq_c < min_c) {
                min_c = seq_c;
            }
            else if(seq_c > max_c) {
                max_c = seq_c;
            }
            if(seq_g < min_g) {
                min_g = seq_g;
            }
            else if(seq_g > max_g) {
                max_g = seq_g;
            }
            if(seq_t < min_t) {
                min_t = seq_t;
            }
            else if(seq_t > max_t) {
                max_t = seq_t;
            }
        }

        jp = new JPanel(new BorderLayout());

        String display = Main.arns.size() + " ARNt, dont "+nb_e_c+" Eukarya, "+nb_a_c+" Archaea, "+nb_b_c+" Bacteria.\n\n";
        display += "Nombres d'apparition des lettres dans les ARNt :\n- A : "+nb_a+"\n- C : "+nb_c+"\n- G : "+nb_g+"\n- T : "+nb_t+"\n- Autres : "+nb_others+"\n\n";
        display += "Il y a au minimum "+min_a+" A, "+min_c+" C, "+min_g+" G, "+min_t+" T par séquence.\n";
        display += "Il y a au maximum "+max_a+" A, "+max_c+" C, "+max_g+" G, "+max_t+" T par séquence.\n";
        display += "\nLa longueur moyenne des séquences est de "+moyenne_length+" caractères.\n";
        display += "La longueur minimale des séquences est de "+min_length+" caractères.\n";
        display += "La longueur maximale des séquences est de "+max_length+" caractères.\n";
        jp.add(new JTextArea(display), BorderLayout.NORTH);

        String[] names = {"A", "C", "G", "T", "Autres"};
        String[] column_id = {"Nombre d'apparition de chaque nucléotide"};
        int data[][] = {{nb_a}, {nb_c}, {nb_g}, {nb_t}, {nb_others}};

        ChartDataModel model = new ObjectChartDataModel(data, column_id, names);

        PieChartRenderer renderer = new PieChartRenderer(model);

        CoordSystem coord = new CoordSystem(model);
        coord.setPaintAxes(false);

        ChartPanel chart = new ChartPanel(model,"Nombre d'apparition de chaque nucléotide");
        chart.addChartRenderer(renderer, 0);
        chart.setCoordSystem(coord);

        jp.add(chart);
    }

    public JPanel getPanel() {
        return jp;
    }
}

